var SignUp={};
if(ys)ys.signup=SignUp;

SignUp.sign=function(form){
	var errorinfo=checkFormValue();
	if(errorinfo){
		toastr.error(errorinfo);
		return false;
	}
	var form=$(form);
	var data =	form.serialize();
	ajaxPromise(function(){
		return $.post("/ajax/signup",data).then(function(data){
			if(data.error){
				toastr.error(data.error);
				return;
			}else if(data.ok){
				document.location.href="/user/"+data.uid;
			}			
		})

	})	
	return false;
}

SignUp.sendMail=function(){
	var email=$("#inputEmail").val();
	if(email)email=email.trim();
	if(!email){
		return emailInfo("你没有填写邮箱");
	}
	var emailReg=/^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,5}$/;
	if(emailReg.test(email)===false) return emailInfo("你没有填写正确的邮箱地址！");
	ajaxPromise(function(){
		return $.post("/ajax/sendmail",{email:email}).then(function(data){
			if(data.error){
				return emailInfo(data.error);
			}else if(data.ok){
				return emailInfo("邮件已发送至"+data.next+"，请去邮箱查收！","success");
			}
		});
	})
}
function emailInfo(msg,type){
	if(!type)type="danger";
	type="bg-"+type;
	var emailInfo=$("#emailInfo");
	emailInfo.attr("class",type);
	emailInfo.text(msg);
}

function checkFormValue(){
	var username=$("#inputUserName").val();
	if(username){
		username=username.trim();
	}
	if(!username)return "你没有填写用户名";
	if(username.length<2) return "你的用户名太短";
	if(username.length>15) return "你的用户名太长";

	var password1=$("#inputPassword1").val();
	var password2=$("#inputPassword2").val();
	if(password1)password1=password1.trim();
	if(!password1) return "请输入你的密码";
	if(password1.length<6) return "你设置的密码太短";
	if(password1!=password2) return "两次填写的密码不一样";

	var email=$("#inputEmail").val();
	if(email)email=email.trim();
	if(!email)return "你没有填写邮箱";
	var emailReg=/^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,5}$/;
	if(emailReg.test(email)===false) return "你没有填写正确的邮箱地址！";
	var emailCode =$("#inputEmailCode").val();
	if(emailCode)emailCode=emailCode.trim();
	if(!emailCode) return "你没有填写邮箱验证码";
	return false;
}